local chongwei = fk.CreateSkill {

  name = "fk__chongwei",

  tags = {  },

}

local U = require "packages/utility/utility"


chongwei:addEffect("active", {
  name = "fk__chongwei",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and player ~= to_select
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(chongwei.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player =effect.from
    local to =effect.tos[1]
    local all_choices = {"#fk__chongwei_change:"..player.id, "#fk__chongwei_hurt:"..player.id}
    local choices = {}
    if to.kingdom ~= player.kingdom then
      table.insert(choices, all_choices[1])
    end
    if #table.filter(player:getCardIds("he"), function(id) return not player:prohibitDiscard(id) end) > 1 then
      table.insert(choices, all_choices[2])
    end
    if #choices == 0 then return end
    local choice = room:askForChoice(to, choices, chongwei.name, "", false, all_choices)
    if choice:startsWith("#fk__chongwei_change") then
      room:changeKingdom(to, player.kingdom, true)
      if player.dead or to.dead then return end
      to:drawCards(2, chongwei.name)
      if player.dead or to.dead then return end
      local cards = to:getCardIds("he")
      if #cards > 2 then
        cards = room:askForCard(to, 2, 2, true, chongwei.name, false, ".", "#fk__chongwei-give:"..player.id)
      end
      room:moveCardTo(cards,Player.Hand,player,fk.ReasonGive,chongwei.name,nil,false,to)
    else
      local cards = room:askForDiscard(player, 2, 999, true, chongwei.name, false, ".", "#fk__chongwei-card:"..to.id)
      if to.dead then return end
      if to:isNude() or room:askForChoice(player, {"#fk__chongwei_throw", "#fk__chongwei_damage"}, chongwei.name) == "#fk__chongwei_damage"
      then
        room:doIndicate(player.id, {to.id})
        room:damage { from = player, to = to, damage = 1, skillName = chongwei.name }
      else
        local x = math.min(#cards, #to:getCardIds("he"))
        local throw = room:askForCardsChosen(player, to, x, x, "he", chongwei.name)
        room:throwCard(throw, chongwei.name, to, player)
      end
    end
  end,
})

return chongwei